package core.reports;

import java.util.Calendar;

import persistence.DataPersistor;
import persistence.ReportDAO;
import persistence.TransactionManager;

import core.ModelObject;
import core.exception.SaveException;

public class ReportData extends ModelObject
{

	private static final long serialVersionUID = 2599765179151778196L;
	private Calendar _removedItemsLastRan;
	private static ReportData _instance = null;
	
	private ReportData() {

	}
	
	public static ReportData instance() {
		if (_instance == null) _instance = new ReportData();
		return _instance;
	}
	
	public static void setInstance(ReportData newInstance) {
		_instance = newInstance;
	}
	
	/**
	 * Sets the time that the Removed Items report was last ran
	 * 
	 * @param calendar the time last ran
	 * @throws SaveException 
	 */
	public void setRemovedItemsRan(Calendar calendar) throws SaveException{
		initRemovedItemsRan(calendar);
			
		DataPersistor db = DataPersistor.instance();
		TransactionManager tm = db.getTransactionManager();
		try {
			tm.beginTransaction();
			ReportDAO rd = db.getReportDAO();
			rd.setRemovedItemsTime(calendar);
			tm.endTransaction(true);
		}
		catch(SaveException e) {
			try {
				tm.endTransaction(false);
			} 
			catch (SaveException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				throw e1;
			}
		}
		
	}
	
	public void initRemovedItemsRan(Calendar calendar) {
		_removedItemsLastRan = calendar;
	}
	
	/**
	 *  Gets the time that the Removed Items report was last ran
	 * 
	 * @return the time last ran
	 */
	public Calendar getRemovedItemsRan() {
		
		return _removedItemsLastRan;
	}
}
